﻿using System;
using Fuse8.ContentManagementFramework.Domain;
using Fuse8.ContentManagementFramework.Domain.Special;
using Fuse8.DomainFramework.CacheService;
using Fuse8.DomainFramework.Domain;

namespace Fuse8.ContentManagementFramework.BizFacade
{
    public class CmsCacheDependency
    {
        public static void Init()
        {
            new CacheConfigurator<CmsPage, int>().DependsOn<CmsTemplate, int>().DependsOn<CmsPageControl, int>().DependsOn<CmsContent, int>();
            new CacheConfigurator<CmsUser, int>().DependsOn<CmsUserGroup, int>();
            new CacheConfigurator<CmsUserInformation, int>().DependsOn<CmsUser, int>();
        }
    }

    internal class CacheConfigurator<TEntity, TKey> where TEntity : class, IEntityObject<TKey>, new() where TKey : IComparable<TKey>, IEquatable<TKey>
    {
        private static CacheFacade cacheFacade = new CacheFacade();

        public CacheConfigurator<TEntity, TKey> DependsOn<TDependentEntity, TDependentKey>()
            where TDependentEntity : class, IEntityObject<TDependentKey>, new()
            where TDependentKey : IComparable<TDependentKey>, IEquatable<TDependentKey>
        {
            cacheFacade.RegisterTypeDependency<TEntity, TKey, TDependentEntity, TDependentKey>();
            return this;
        }
    }
}